﻿package baseEngine.basic
{
    import __AS3__.vec.*;
    import baseEngine.core.*;
    import flash.utils.*;

    public class RenderList extends Object
    {
        public var layMap:Dictionary;
        public var layers:Vector.<Layer3DSort>;

        public function RenderList()
        {
            this.layMap = new Dictionary(false);
            this.layers = new Vector.<Layer3DSort>;
            return;
        }// end function

        public function clear() : void
        {
            var _loc_1:Layer3DSort = null;
            for each (_loc_1 in this.layMap)
            {
                
                _loc_1.list.length = 0;
            }
            this.layers.length = 0;
            this.layMap = new Dictionary(false);
            return;
        }// end function

        public function insertIntoList(param1:Mesh3D) : Layer3DSort
        {
            var _loc_4:int = 0;
            var _loc_2:* = param1.layer;
            var _loc_3:* = this.layMap[_loc_2];
            if (_loc_3 == null)
            {
                var _loc_5:* = new Layer3DSort(_loc_2);
                _loc_3 = new Layer3DSort(_loc_2);
                this.layMap[_loc_2] = _loc_5;
                _loc_4 = RenderList.fastGetIndexByLayerId(this.layers, _loc_2);
                this.layers.splice(_loc_4, 0, _loc_3);
            }
            if (_loc_3.list.indexOf(param1) == -1)
            {
                _loc_3.list.push(param1);
                _loc_3.isActive = true;
            }
            return _loc_3;
        }// end function

        public function removeFromList(param1:Mesh3D) : Layer3DSort
        {
            var _loc_2:* = this.layMap[param1.layer];
            if (!_loc_2)
            {
                return null;
            }
            var _loc_3:* = _loc_2.list.indexOf(param1);
            if (_loc_3 != -1)
            {
                _loc_2.list.splice(_loc_3, 1);
            }
            return _loc_2;
        }// end function

        private static function fastGetIndexByLayerId(param1:Vector.<Layer3DSort>, param2:int) : int
        {
            var _loc_6:int = 0;
            var _loc_3:* = param1.length;
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            while (_loc_4 < _loc_3)
            {
                
                _loc_5 = _loc_4 + _loc_3 >> 1;
                _loc_6 = param1[_loc_5].layerId;
                if (_loc_6 == param2)
                {
                    _loc_3 = _loc_5;
                    break;
                }
                if (param2 > _loc_6)
                {
                    _loc_4 = _loc_5 + 1;
                    continue;
                }
                _loc_3 = _loc_5 + 1;
            }
            return _loc_3;
        }// end function

    }
}
